package cinema;

import java.util.Date;
import java.util.List;
import java.util.Objects;

public class User {
    private final String userId;
    private final String username;
    private String password;
    private final Date registerTime;
    private String userType; // 管理员/经理/前台
    private String phone;

    public User(String userId, String username, String password,
                Date registerTime, String userType, String phone) {
        this.userId = Objects.requireNonNull(userId, "用户ID不能为空");
        this.username = Objects.requireNonNull(username, "用户名不能为空");
        this.password = validatePassword(password);
        this.registerTime = new Date(Objects.requireNonNull(registerTime, "注册时间不能为空").getTime()); // 防御性拷贝
        this.userType = validateUserType(userType);
        this.phone = validatePhone(phone);
    }

    // 用户登录验证
    public boolean login(String inputUsername, String inputPassword) {
        return username.equals(inputUsername) && password.equals(inputPassword);
    }

    // 用户退出登录
    public void logout() {
        System.out.println(username + " 已退出登录");
    }

    // 修改密码
    public boolean changePassword(String oldPassword, String newPassword) {
        if (!password.equals(oldPassword)) {
            System.out.println("旧密码不正确");
            return false;
        }

        password = validatePassword(newPassword);
        System.out.println("密码修改成功");
        return true;
    }

    //============ Getter 和 Setter 方法 ============//
    public String getUserId() {
        return userId;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = validatePassword(password);
    }

    public Date getRegisterTime() {
        return new Date(registerTime.getTime()); // 返回副本防止外部修改
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = validateUserType(userType);
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = validatePhone(phone);
    }

    //============ 辅助方法 ============//
    private String validatePassword(String password) {
        if (password == null || password.length() < 6) {
            throw new IllegalArgumentException("密码长度至少为6位");
        }
        return password;
    }

    private String validateUserType(String userType) {
        if (!List.of("管理员", "经理", "前台").contains(userType)) {
            throw new IllegalArgumentException("无效的用户类型");
        }
        return userType;
    }

    private String validatePhone(String phone) {
        if (phone == null || !phone.matches("\\d{11}")) {
            throw new IllegalArgumentException("手机号格式无效");
        }
        return phone;
    }

    @Override
    public String toString() {
        return String.format("""
                ID: %s
                用户名: %s
                用户类型: %s
                注册时间: %s
                手机号: %s""",
                userId, username, userType, registerTime, phone);
    }
}